#include "spi.h"



uint8_t SPI_Slave_Buffer_Rx[CMD_LEN]={0};
//uint8_t SPI_Slave_Buffer_Tx[CMD_LEN];
uint8_t SPI_Slave_Buffer_Tx[CMD_LEN] ={0x47, 0xC3, 0x4F, 0xFF, 0x40, 0x1C, 0x3F, 0xFF,
								0x56, 0x78, 0x56, 0x78, 0x78, 0x56, 0x78, 0x56,
								0x56, 0x78, 0x56, 0x78, 0x78, 0x56, 0x78, 0x56,
								0x56, 0x78, 0x56, 0x78, 0x78, 0x56, 0x78, 0x56,
								0x56, 0x78, 0x56, 0x78, 0x78, 0x56, 0x78, 0x56,
								0x56, 0x78, 0x56, 0x78, 0x78, 0x56, 0x78, 0x56,
								0x56, 0x78, 0x56, 0x78, 0x78, 0x56, 0x78, 0x56,
								0x56, 0x78, 0x56, 0x78, 0x78, 0x56, 0x78, 0x56,
								0x56, 0x78, 0x56, 0x78, 0x78, 0x56, 0x78, 0x56,
								0x56, 0x78, 0x56, 0x78, 0x78, 0x56, 0x78, 0x56,
								0x40, 0x1C, 0x3F, 0xFF, 0x40, 0x1C, 0x3F, 0xFF,
								0x40, 0x1C, 0x3F, 0xFF};

void spi_gpio_configuration(void)
{
    gpio_init_type gpio_init_struct;

    gpio_default_para_init(&gpio_init_struct);

    /* configure slave pins SCK */
    gpio_init_struct.gpio_pins = SPI_Slave_PIN_SCK;
    gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
    gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    gpio_init(SPI_Slave_GPIO, &gpio_init_struct);

    /* configure slave pin MISO*/
    gpio_init_struct.gpio_pins = SPI_Slave_PIN_MISO;
    gpio_init_struct.gpio_pull = GPIO_PULL_UP;
    gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
    gpio_init(SPI_Slave_GPIO, &gpio_init_struct);

    /* configure slave pins MOSI*/
    gpio_init_struct.gpio_pins = SPI_Slave_PIN_MOSI;
    gpio_init_struct.gpio_pull = GPIO_PULL_UP;
    gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    gpio_init(SPI_Slave_GPIO, &gpio_init_struct);

    /* configure slave pin CS*/
    gpio_init_struct.gpio_pins = SPI_Slave_PIN_NSS;
    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
    gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    gpio_init(GPIOD, &gpio_init_struct);
}

void spi_configuration(void)
{
    dma_init_type dma_init_struct;
    spi_init_type spi_init_struct;

    /* SPI_Slave_Tx_DMA_Channel configuration ---------------------------------------------*/
    dma_reset(SPI_Slave_Tx_DMA_Channel);
    dma_default_para_init(&dma_init_struct);
    dma_init_struct.buffer_size = CMD_LEN;
    dma_init_struct.direction = DMA_DIR_MEMORY_TO_PERIPHERAL;
    dma_init_struct.memory_base_addr = (uint32_t)SPI_Slave_Buffer_Tx;
    dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;
    dma_init_struct.memory_inc_enable = TRUE;
    dma_init_struct.peripheral_base_addr = (uint32_t)&SPI_Slave->dt;
    dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
    dma_init_struct.peripheral_inc_enable = FALSE;
    dma_init_struct.priority = DMA_PRIORITY_HIGH;
    dma_init_struct.loop_mode_enable = TRUE;
    dma_init(SPI_Slave_Tx_DMA_Channel, &dma_init_struct);

    /* SPI_Slave_Rx_DMA_Channel configuration ---------------------------------------------*/
    dma_reset(SPI_Slave_Rx_DMA_Channel);
    dma_default_para_init(&dma_init_struct);
    dma_init_struct.buffer_size = CMD_LEN;
    dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY;
    dma_init_struct.memory_base_addr = (uint32_t)SPI_Slave_Buffer_Rx;
    dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;
    dma_init_struct.memory_inc_enable = TRUE;
    dma_init_struct.peripheral_base_addr = (uint32_t)&SPI_Slave->dt;
    dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
    dma_init_struct.peripheral_inc_enable = FALSE;
    dma_init_struct.priority = DMA_PRIORITY_VERY_HIGH;
    dma_init_struct.loop_mode_enable = TRUE;
    dma_init(SPI_Slave_Rx_DMA_Channel, &dma_init_struct);

    /* SPI_Slave configuration ------------------------------------------------------*/
    spi_default_para_init(&spi_init_struct);
    spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
    spi_init_struct.master_slave_mode = SPI_MODE_SLAVE;
    spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_2;
    spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
    spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
    spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_HIGH;
    spi_init_struct.clock_phase = SPI_CLOCK_PHASE_1EDGE;
    spi_init_struct.cs_mode_selection = SPI_CS_HARDWARE_MODE;
    spi_init(SPI_Slave, &spi_init_struct);

    /* Enable SPI_Slave Tx Rx request */
    spi_i2s_dma_transmitter_enable(SPI_Slave, TRUE);
    spi_i2s_dma_receiver_enable(SPI_Slave, TRUE);


	/* Enable SPI_Slave_Rx_DMA_Channel */
    dma_channel_enable(SPI_Slave_Rx_DMA_Channel, TRUE);
    /* Enable SPI_Slave_Tx_DMA_Channel */
    dma_channel_enable(SPI_Slave_Tx_DMA_Channel, TRUE);
		
       /* Enable SPI_Slave */
    spi_enable(SPI_Slave, TRUE);
}


void SPI_Init(void)
{
    spi_gpio_configuration();
    spi_configuration();
}

void SPI_DMA_Send(void)
{
     
}